#include <websocket.hpp>

using namespace leak;

void EchoServer(utils::ContextPool pool) {
    net::ws::Handlers handlers;
    handlers.connect_handler = [](std::shared_ptr<net::tcp::Socket> socket) {
        std::cout << "server ws connect" << std::endl;
    };

    handlers.message_handler = [](std::shared_ptr<net::tcp::Socket> socket, std::string_view data) {
        std::cout << "server ws message: " << data << std::endl;
    };

    handlers.error_handler = [](std::shared_ptr<net::tcp::Socket> socket, std::string_view err) {
        std::cout << "server ws error: " << err << std::endl;
    };

    net::ws::NewServer(pool, "0.0.0.0", 12345, std::move(handlers));
}

void EchoClient(utils::ContextPool pool, size_t count) {
    net::ws::Handlers handlers;
    handlers.connect_handler = [](std::shared_ptr<net::tcp::Socket> socket) {
        std::cout << "client ws connect" << std::endl;
    };

    handlers.message_handler = [](std::shared_ptr<net::tcp::Socket> socket, std::string_view data) {
        std::cout << "client ws message: " << data << std::endl;
    };

    handlers.error_handler = [](std::shared_ptr<net::tcp::Socket> socket, std::string_view err) {
        std::cout << "client ws error: " << err << std::endl;
    };

    net::ws::Connect(pool->Next(), "127.0.0.1", 12345, std::move(handlers));
}

int main(int argc, char* argv[]) {
    auto pool = std::make_shared<utils::SequenceContextPool>(1);
    EchoServer(pool);
    EchoClient(pool, 1);
    getchar();
    return 0;
}